package io.jpress.commons.word;

import com.jfinal.kit.LogKit;
import org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException;

import java.io.File;

public class POIUtil {


    public static String parse(File file) {
        try {
            return doParse(file);
        } catch (Exception e) {
            LogKit.error(e.toString(), e);
        }
        return "";
    }


    public static String doParse(File file) throws Exception {
        String name = file.getName();
        String result = null;

        //try
        if (name.toLowerCase().endsWith(".doc")) {
            try {
                result = HWPFUtil.parse(file);
            } catch (IllegalArgumentException e) {
                String message = e.getMessage();
                if (message != null && message.contains("OOXML")) {
                    result = XWPFUtil.parse(file);
                }
            }
        }

        if (name.toLowerCase().endsWith(".docx")) {
            try {
                result = XWPFUtil.parse(file);
            } catch (NotOfficeXmlFileException e) {
                result = HWPFUtil.parse(file);
            }
        }

        return result;
    }


    public static void main(String[] args) throws Exception {
//        String parse = POIUtil.parse(new File("/Users/michael/Desktop/项目/test.doc"));
//        String parse = POIUtil.parse(new File("/Users/michael/Desktop/项目/test_e.docx"));
//        String parse = POIUtil.parse(new File("/Users/michael/Desktop/项目/test22_e.doc"));
        String parse = POIUtil.parse(new File("/Users/michael/Desktop/项目/test22.docx"));
        System.out.println(parse);
//    XDocReportUtil.parse(new File("/Users/michael/Desktop/项目/test22.docx"));
//        System.out.println(Str);
    }

}
